﻿using System;
using UnityEngine;
using UnityEngine.UI;

namespace Nomo.UnityCoreModule
{
    /// <summary>
    /// 网格布局组适配器。
    /// </summary>
    public readonly struct GridLayoutGroupAdaptor
    {
        /// <summary>
        /// 
        /// </summary>
        public readonly GridLayoutGroup GridLayoutGroup;

        /// <summary>
        /// 
        /// </summary>
        public readonly RectTransform RectTransform;

        /// <summary>
        /// 
        /// </summary>
        public readonly RectTransform AdaptTo;

        /// <summary>
        /// 
        /// </summary>
        public readonly int RowCount;

        /// <summary>
        /// 
        /// </summary>
        public readonly int ColumnCount;

        /// <summary>
        /// 
        /// </summary>
        public readonly float CellWidth;

        /// <summary>
        /// 
        /// </summary>
        public readonly float CellHeight;

        /// <summary>
        /// 
        /// </summary>
        public readonly float SpacingWidth;

        /// <summary>
        /// 
        /// </summary>
        public readonly float SpacingHeight;

        /// <summary>
        /// 
        /// </summary>
        public readonly int Padding;

        /// <summary>
        /// 
        /// </summary>
        public readonly float Margin;

        /// <summary>
        /// 
        /// </summary>
        public readonly float MapWidthRatio;

        /// <summary>
        /// 
        /// </summary>
        public readonly float MapHeightRatio;

        /// <summary>
        /// 
        /// </summary>
        public readonly Vector2 PreferredSize;

        static GridLayoutGroupAdaptor()
        {
            throw new NotImplementedException($"{nameof(GridLayoutGroupAdaptor)} 类型处于未完成阶段。");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gridLayoutGroup"></param>
        /// <param name="adaptTo"></param>
        /// <param name="rowCount"></param>
        /// <param name="columnCount"></param>
        /// <param name="cellWidth"></param>
        /// <param name="cellHeight"></param>
        /// <param name="spacingWidth"></param>
        /// <param name="spacingHeight"></param>
        /// <param name="padding"></param>
        /// <param name="margin"></param>
        /// <param name="mapWidthRatio"></param>
        /// <param name="mapHeightRatio"></param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="NotImplementedException"></exception>
        public GridLayoutGroupAdaptor(GridLayoutGroup gridLayoutGroup, RectTransform adaptTo, int rowCount, int columnCount, float cellWidth, float cellHeight, float spacingWidth, float spacingHeight, int padding, float margin, float mapWidthRatio, float mapHeightRatio)
        {
            if (gridLayoutGroup == null)
            {
                throw new ArgumentNullException(nameof(gridLayoutGroup));
            }

            if (!(gridLayoutGroup.transform is RectTransform))
            {
                throw new InvalidOperationException($"{nameof(gridLayoutGroup)} 所在的游戏物体的变换不是矩形变换。");
            }

            if (adaptTo == null)
            {
                throw new ArgumentNullException(nameof(adaptTo));
            }

            if (rowCount < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(rowCount));
            }

            if (columnCount < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(columnCount));
            }

            GridLayoutGroup = gridLayoutGroup;
            RectTransform   = (RectTransform) gridLayoutGroup.transform;
            AdaptTo         = adaptTo;
            RowCount        = rowCount;
            ColumnCount     = columnCount;
            CellWidth       = cellWidth;
            CellHeight      = cellHeight;
            SpacingWidth    = spacingWidth;
            SpacingHeight   = spacingHeight;
            Padding         = padding;
            Margin          = margin;
            MapWidthRatio   = mapWidthRatio;
            MapHeightRatio  = mapHeightRatio;

            PreferredSize = default; // TODO

            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public GridLayoutGroupAdaptor SetLayout()
        {
            throw new NotImplementedException();
            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public GridLayoutGroupAdaptor SetSize()
        {
            throw new NotImplementedException();
            return this;
        }
    }
}
